/**
 * 
 */
package com.dhcc.DRGsHosp.application.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.querychannel.JdbcTemplateObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.FieldMapApplication;
import com.dhcc.DRGsHosp.infra.util.StringUtils;

/**
 * 
 * @ClassName: FieldMapApplicationImpl  
 * @Description ：    
 * @Date: 2018年1月19日 上午10:13:44  
 * @Author mazhaoyan 
 * @Version 2.5.0 DHC-DRGs
 */
@Named
@Transactional
public class FieldMapApplicationImpl implements FieldMapApplication {
	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	

	/**  
	 *  @Description：
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.FieldMapApplication#getFieldMap(java.lang.String, java.lang.String, java.lang.String)  
	 */
	@Override
	public List<Map<String,Object>> getFieldMap(String fileType, String tableCode) {
		List<Map<String, Object>> list =new LinkedList<Map<String,Object>>();
		Map<String, Object> param = new HashMap<String,Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT T.TITLE_NAME,T.TABLE_CODE,T.FIELD_CODE,T.FIELD_TYPE,T.FIELD_ORDER  FROM DRGS.T_FIELD_MAP T   \n ");
		sql.append(" WHERE T.FILE_TYPE = :fileType AND T.TABLE_CODE = :tableCode  \n ");
		param.put("fileType",fileType);
		param.put("tableCode",tableCode);
		list=jdbcTempSeverice.queryForList(sql.toString(),param);
		return list;
	}

	/**  
	 *  @Description：
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.FieldMapApplication#getKeyMap(java.lang.String)  
	 */
	@Override
	public List<Map<String, Object>> getKeyMap(String fileType) {
		List<Map<String, Object>> list =new LinkedList<Map<String,Object>>();
		Map<String, Object> param = new HashMap<String,Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT FM.TITLE_NAME,FM.FIELD_CODE FROM DRGS.T_FIELD_MAP FM WHERE FM.FIELD_CODE IN ('P00050000','P00040000','P00290000') AND FM.FILE_TYPE = :fileType  \n ");
		param.put("fileType",fileType);
		list=jdbcTempSeverice.queryForList(sql.toString(),param);
		return list;
	}
/**
 *  @Description：获取字段映射关系
 *  @Author：liuhongjun 
 *  @see com.dhcc.DRGsHosp.application.FieldMapApplication#getTableData(java.lang.String)
 */
	@Override
	public Object getTableData(String fileType) {
		return jdbcTemplateObj.queryForList("select TITLE_NAME,TABLE_CODE,FIELD_CODE,FIELD_TYPE,FIELD_ORDER,TITLE_DESC from drgs.t_field_map fm where fm.file_type = '"+fileType+"'");
	}
/**
 *  @Description：保存字段信息
 *  @Author：liuhongjun 
 *  @see com.dhcc.DRGsHosp.application.FieldMapApplication#saveSingleField(java.util.Map)
 */
@Override
public void saveSingleField(Map<String, Object> mapInfo) {
	String fileType=mapInfo.get("fileType")==null?null:mapInfo.get("fileType").toString();
	String titleName=mapInfo.get("fieldName")==null?null:mapInfo.get("fieldName").toString();
	String tableCode=mapInfo.get("tableCode")==null?null:mapInfo.get("tableCode").toString();
	String fieldCode=mapInfo.get("fieldCode")==null?null:mapInfo.get("fieldCode").toString();
	String fieldType=mapInfo.get("fieldType")==null?null:mapInfo.get("fieldType").toString();
	String dataFormate=mapInfo.get("dataFormate")==null?null:mapInfo.get("dataFormate").toString();
	String fieldOrder=mapInfo.get("fieldOrder")==null?null:mapInfo.get("fieldOrder").toString();
	String fieldDesc=mapInfo.get("fieldDesc")==null?null:mapInfo.get("fieldDesc").toString();
	StringBuilder lineSql = new StringBuilder();
	lineSql.append("declare cnt INTEGER; begin \n");
	lineSql.append(" select count(*) into cnt from drgs.t_field_map fm \n");
	lineSql.append(" where fm.file_type = '"+fileType+"' and fm.title_name = '"+titleName+"' \n");
	lineSql.append(" and fm.field_code = '"+fieldCode+"' and fm.table_code = '"+tableCode+"' ; \n");
	lineSql.append(" if cnt = 0 then \n");
	lineSql.append(" insert into drgs.t_field_map (file_type,data_format,TITLE_NAME,TABLE_CODE,FIELD_CODE,FIELD_TYPE,FIELD_ORDER,TITLE_DESC) \n");
	lineSql.append(" values ('"+fileType+"','"+dataFormate+"','"+titleName+"','"+tableCode+"','"+fieldCode+"','"+fieldType+"','"+fieldOrder+"','"+fieldDesc+"');  \n ");
	lineSql.append(" else \n ");
	lineSql.append(" update drgs.t_field_map fm set fm.TABLE_CODE = '"+tableCode+"', fm.FIELD_TYPE = '"+fieldType+"', fm.FIELD_ORDER = '"+fieldOrder+"' ,fm.data_format = '"+dataFormate+"' ,fm.TITLE_DESC='"+fieldDesc+"' \n ");
	lineSql.append(" where   fm.file_type = '"+fileType+"' and fm.TITLE_NAME = '"+titleName+"' and fm.FIELD_CODE = '"+fieldCode+"' and fm.table_code = '"+tableCode+"'  ; \n ");
	lineSql.append(" end if; \n ");
	lineSql.append("commit; \n ");
	lineSql.append("end; \n ");
	jdbcTemplateObj.execute(lineSql.toString());	
   }
/**
 *  @Description：删除字段映射
 *  @Author：liuhongjun 
 *  @see com.dhcc.DRGsHosp.application.FieldMapApplication#deleteFieldMap(java.util.Map)
 */
@Override
public void deleteFieldMap(Map<String, Object> mapInfo) {
	String fileType=mapInfo.get("fileType")==null?null:mapInfo.get("fileType").toString();
	String fileName=mapInfo.get("fieldName")==null?null:mapInfo.get("fieldName").toString();
	String fieldCode=mapInfo.get("fieldCode")==null?null:mapInfo.get("fieldCode").toString();
	jdbcTemplateObj.execute("delete drgs.t_field_map fm where fm.file_type = '"+fileType+"' and fm.title_name = '"+fileName+"' and fm.field_code = '"+fieldCode+"' \n");
 }
/**
 *  @Description：保存字段映射
 *  @Author：liuhongjun 
 *  @see com.dhcc.DRGsHosp.application.FieldMapApplication#saveTableFiled(java.lang.StringBuilder)
 */
@Override
public void saveTableFiled(StringBuilder saveSql) {
	jdbcTemplateObj.execute(saveSql.toString())	;
}
}

